﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using whatpricemyhouse.LINQ;

namespace whatpricemyhouse
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ImageButton1.Attributes.Add("onmouseover", "mouseOver1(this);");
            ImageButton1.Attributes.Add("onmouseout", "mouseOut1(this);");

            ImageButton2.Attributes.Add("onmouseover", "mouseOver2(this);");
            ImageButton2.Attributes.Add("onmouseout", "mouseOut2(this);");


            this.inputString.Attributes.Add("onkeyup", "lookup(this.value);");
            if (!String.IsNullOrEmpty(Request.QueryString["c"]))
            {
                this.inputString.Value = Request.QueryString["c"].ToString();
            }
            if (!String.IsNullOrEmpty(Request.QueryString["e"]))
            {
                this.ErrorLabel.Text = "Region must be inputed!";
            }
            else {
                this.ErrorLabel.Text = "";
            }
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            if (checkArea())
            {
                Session["areaName"] = this.inputString.Value;
                Response.Redirect("~/Sell/ReportSelectPage.aspx");
            }
            else {
                Response.Redirect("~/default.aspx");
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (checkArea())
            {
                Session["areaName"] = this.inputString.Value;
                Response.Redirect("~/Buy/BuyForm.aspx");
            }
            else {
                Response.Redirect("~/default.aspx");
            }
        }


        private bool checkArea()
        {
            bool hasArea = false;
            DataControllerDataContext dcdc = new DataControllerDataContext();
            var regionList = from r in dcdc.suburbs
                             where r.area == this.inputString.Value
                             select r;
            if (regionList != null)
            {
                if (regionList.Count() > 0)
                {
                    hasArea = true;
                }
            }
            return hasArea;
        }
    }
}